package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class Ingreso_Comentarios extends JPanel {

	private static final long serialVersionUID = -2506969618843776850L;
	
	private JPanel 			 p1, p2;
	private JLabel			 lab;
	private JTextArea        texto;
	private AbstractDocument pDoc;
	private JScrollPane      sc;
	private final int  		 max_char = 255;
	
	public Ingreso_Comentarios(){
		setLayout(new BorderLayout());			
		setBorder(BorderFactory.createTitledBorder("Mensaje"));

		// Construyo p1
		p1 = new JPanel();
		lab = new JLabel("Maxima cantidad de caracteres: " + String.valueOf(max_char), FlowLayout.LEFT);
		p1.add(lab);
		
		// Construyo p2
		p2 = new JPanel(new BorderLayout());	
		
		texto = new JTextArea(5, 5);
		texto.setSize(100, 50);		
		texto.setLineWrap(true);
		texto.setWrapStyleWord(true);

		pDoc = (AbstractDocument)texto.getDocument();
		pDoc.setDocumentFilter(new DocumentSizeFilter(max_char));

		sc = new JScrollPane(texto);
		p2.add(sc, BorderLayout.CENTER);
		
		add(p1, BorderLayout.NORTH);
		add(p2, BorderLayout.CENTER);
		
	}
	
	public String getMensaje(){
		return texto.getText();
	}
	
	private class DocumentSizeFilter extends DocumentFilter {
	    int maxCharacters;
	    boolean DEBUG = false;

	    public DocumentSizeFilter(int maxChars) {
	        maxCharacters = maxChars;
	    }

	    public void insertString(FilterBypass fb, int offs,
	                             String str, AttributeSet a)
	        throws BadLocationException {
	        if (DEBUG) {
	            System.out.println("in DocumentSizeFilter's insertString method");
	        }

	        //This rejects the entire insertion if it would make
	        //the contents too long. Another option would be
	        //to truncate the inserted string so the contents
	        //would be exactly maxCharacters in length.
	        if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
	            super.insertString(fb, offs, str, a);
	        else
	            Toolkit.getDefaultToolkit().beep();
	    }
	    
	    public void replace(FilterBypass fb, int offs,
	                        int length, 
	                        String str, AttributeSet a)
	        throws BadLocationException {
	        if (DEBUG) {
	            System.out.println("in DocumentSizeFilter's replace method");
	        }
	        //This rejects the entire replacement if it would make
	        //the contents too long. Another option would be
	        //to truncate the replacement string so the contents
	        //would be exactly maxCharacters in length.
	        if ((fb.getDocument().getLength() + str.length()
	             - length) <= maxCharacters)
	            super.replace(fb, offs, length, str, a);
	        else
	            Toolkit.getDefaultToolkit().beep();
	    }

	}
	
}
